package com.office.onlineInformation.unread.controller;


import com.office.onlineInformation.unread.pojo.OaOnlineMessage;
import com.office.onlineInformation.unread.service.UnsendService;
import com.office.personalOffice.personalInfo.model.LoginUser;
import com.office.personalOffice.personalInfo.model.TbStaff;
import com.office.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 88168
 * @since 2022-10-25
 */
@RestController
@RequestMapping("/unread")
public class UnreadController {
    @Autowired
    public UnsendService service;

    /**
     * 删除未读留言
     *
     * @param informationId
     * @return
     */
    @RequestMapping("/delete/{informationId}")
    public String deleteMessage(@PathVariable int informationId) {
        String message = service.deleteMessage(informationId);
        return message;
    }

    /**
     * 回复留言
     *
     * @param onlineMessage
     * @return
     */
    @RequestMapping("/update")
    public String updateMessage(@RequestBody OaOnlineMessage onlineMessage) {
        String message = service.updateMessage(onlineMessage);
        return message;
    }

    /**
     * 查询未读留言
     *
     * @return
     */
    @RequestMapping("/get")
    public PageModel<OaOnlineMessage> getList(OaOnlineMessage customManage, PageModel<OaOnlineMessage> pageModel, Authentication authentication) {
        TbStaff user = getUser(authentication);
        customManage.setSender(user.getName());
        PageModel<OaOnlineMessage> list = service.getList(customManage, pageModel);
        return pageModel;
    }

    /**
     * 根据id查询未读留言
     *
     * @param informationId
     * @return
     */
    @RequestMapping("/getById/{informationId}")
    public OaOnlineMessage getById(@PathVariable int informationId) {
        OaOnlineMessage message = service.getById(informationId);
        return message;
    }

    public static TbStaff getUser(Authentication authentication){
        LoginUser principal =(LoginUser) authentication.getPrincipal();
        TbStaff user = principal.getUser();
        return user;
    }

}

